//
//  DYZSelectCarsTypeController.m
//  Demo
//
//  Created by 毕仲阳 on 16/8/19.
//  Copyright © 2016年 mycompany. All rights reserved.
//

#import "DYZSelectCarsTypeController.h"
#import "DYZLaunchCarsController.h"
#import "DSHTTPClient.h"
#import "DYZLaunCarsModel.h"

static NSString *carsTypeID = @"carsTypeID";
@interface DYZSelectCarsTypeController ()

@property(nonatomic,strong)NSArray *carType;
@end

@implementation DYZSelectCarsTypeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self model];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:carsTypeID];
    self.navigationItem.title = @"选择车型";
    //    NSArray *carsType = @[@{@"carType":@"奔驰C200L 15款 旧款"},
    //                          @{@"carType":@"奔驰C260L 15款"},
    //                          @{@"carType":@"奔驰C200L 15款 运动 旧款"},
    //                          @{@"carType":@"奔驰C260L 15款 旧款"}];
    //    self.carType = carsType;
}

//型号
- (void)model{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"1" forKey:@"accountid"];
    [param setValue:self.carId forKey:@"carLineId"];
    
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/car/model.json.php" withParam:param withSuccessBlock:^(id data) {
        
        NSMutableArray *array = [NSMutableArray array];
        
        NSDictionary *dict = [data objectForKey:@"data"];
        
        NSArray *arr = [dict objectForKey:@"carList"];
        
        //        NSLog(@"%@",arr);
        
        for (NSDictionary *dict1 in arr) {
            
            NSArray *arr2 = [dict1 objectForKey:@"modelList"];
            //            NSLog(@"%@",[arr2 yy_modelDescription]);
            
            for (NSDictionary *dict2 in arr2) {
                
                DYZLaunCarsModel *model = [DYZLaunCarsModel yy_modelWithDictionary:dict2];
                
                [array addObject:model];
            }
            
            
        }
        //        NSLog(@"%@",[array yy_modelDescription]);
        self.carType = array;
        [self.tableView reloadData];
        
        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//获取车型
- (void)sourcetype{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/car/sourcetype.json.php" withParam:param withSuccessBlock:^(NSDictionary * data) {
        
        NSDictionary *dict = [data objectForKey:@"data"];
        NSMutableArray *array = [NSMutableArray array];
        
        [array addObject:@"不限"];
        
        NSArray *arr = [dict objectForKey:@"attrList"];
        
        for (NSDictionary *dict1 in arr) {
            
            NSString *str = [dict1 objectForKey:@"attrName"];
            
            [array addObject:str];
            
        }
        //        NSLog(@"%@",array);
        self.carType = array.copy;
        [self.tableView reloadData];
        
        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
        
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    
    
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.carType.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:carsTypeID forIndexPath:indexPath];
    DYZLaunCarsModel *model = self.carType[indexPath.row];
    
    NSString *str1 = [self.label stringByAppendingFormat:@"%@",model.carModelName];
    
    //    cell.textLabel.text = self.carType[indexPath.row];
    cell.textLabel.text = str1;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}

@end
